﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace YemekteyizLib.Bs
{
    public class GrupProvider
    {
        public int UrunEkle(Grup g, Urun u)
        {
            g.Urunleri.Add(u);
            return 1;
        }

        public static List<Grup> GetByRestoran(Restoran r)
        {
            SqlCommand cmd = new SqlCommand("select * from UrunGruplar where RestoranID=@ID");
            cmd.Parameters.AddWithValue("@ID", r.ID);
            cmd.Connection = Tools.GetConnection();

          List<Grup> donecekgrup =new List<Grup>();
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                Grup g = new Grup();
                g.Adi = rdr.GetString(1);
                g.Sirasi = Convert.ToInt32(rdr[2]);
                g.AktifMi = rdr.GetBoolean(3);
                g.ID = rdr.GetInt32(0);
                donecekgrup.Add(g);
            }
        
            
            cmd.Connection.Close();
            return donecekgrup;

    
        
        
        }
        public static Grup GetById(int id)
        {
            SqlCommand cmd = new SqlCommand("select * from UrunGruplar where GrupID=@ID");
            cmd.Parameters.AddWithValue("@ID", id);

            cmd.Connection = Tools.GetConnection();

            SqlDataReader rdr = cmd.ExecuteReader();

            rdr.Read();
            Grup g = new Grup();
            g.ID = id;
            g.Adi = rdr.GetString(1);
            g.Sirasi = Convert.ToInt32(rdr[2]);
            g.AktifMi = rdr.GetBoolean(3);
            int restoranID = rdr.GetInt32(4);
            
            cmd.Connection.Close();

            g.Restorani = RestoranProvider.GetByID(restoranID);

            return g;
        }


        public static void GrupEkle(Grup g)
        {
            SqlCommand cmd = new SqlCommand("insert into UrunGruplar(Adi,Sirasi,AktifMi,RestoranID) values(@Adi,@Sirasi,@Aktifmi,@ResID");
            cmd.Parameters.AddWithValue("@Adi", g.Adi);
            cmd.Parameters.AddWithValue("@Sirasi", g.Sirasi);
            cmd.Parameters.AddWithValue("@AktifMi", g.AktifMi);
            cmd.Parameters.AddWithValue("@ResID", g.Restorani.ID);
            cmd.Connection = Tools.GetConnection();
            cmd.ExecuteNonQuery();
            Tools.CloseConnection();
            
            
            
        }

        public static void GrupKaydet(Grup g)
        {
            SqlCommand cmd = new SqlCommand("update UrunGruplar set Adi=@p1,Sirasi=@p2,AktifMi=@p3,RestoranID=p4 where GrupID=@ID");
            cmd.Parameters.AddWithValue("@p1",g.Adi);
            cmd.Parameters.AddWithValue("@p2",g.Sirasi);
            cmd.Parameters.AddWithValue("@p3",g.AktifMi);
            cmd.Parameters.AddWithValue("@p4",g.Restorani.ID);
            cmd.Parameters.AddWithValue("@ID", g.ID);

            cmd.Connection = Tools.GetConnection();
            cmd.ExecuteNonQuery();
            Tools.CloseConnection();
        }

        public static void GrupSil(Grup g)
        {
            SqlCommand cmd = new SqlCommand("delete UrunGruplar where GrupID=@ID");
            cmd.Parameters.AddWithValue("@ID", g.ID);
            cmd.Connection = Tools.GetConnection();
            cmd.ExecuteNonQuery();
            Tools.CloseConnection();

        
        
        }

    }
}
